<?php
class CreateController extends CController{
    //新規登録action
    public function actionStep2(){
        $model = new Profile;
        if(isset($_POST['Profile'])){
            $model->attributes = $_POST['Profile'];
            $model->validate();
            if($model->validate()){
                Yii::app()->session['step2']=$model;
                $transaction=$model->dbConnection->beginTransaction();
                try{
                    $step1=Yii::app()->session['step1'];
                    $step1->save();
                    $user = User::model()->findByAttributes(array('username'=>$step1->username));
                    $id = $user->id;
                    $model->id=$id;
                    $model->formatBirthday();
                    if($model->save()){
                        $transaction->commit();
                        $this->redirect(array('manager/create/success','username'=>$user->username));
                    }else{
                        $transaction->rollBack();
                    }
                }catch(Exception $e){
                    $transaction->rollBack();
                }
            }
        }
        $month = $this->generateMonth();
        $day = $this->generateDay();
        $this->layout = '_layout';
        $this->render('step2',array('title'=>'Create Step2','model'=>$model,'month'=>$month,'day'=>$day));
    }
    public function actionStep1(){
       $model = new User;
       if(isset($_POST['User'])){
           $model->attributes = $_POST['User'];
            if($model->validate()){
                Yii::app()->session['step1']=$model;
                $this->redirect(array('manager/create/step2'));
            }
        }
        $this->layout = '_layout';
        $this->render('step1',array('title'=>'Create Step1','model'=>$model));
    }
    public function actionSuccess(){
        $this->layout = '_layout';
        $this->render('success',  array('title'=>'Create Success','model'=>$_GET['username']));
    }
    private function generateMonth(){
        $item;
        for($i=1;$i<=12;$i++){
            $item[$i]=$i;
        }
        return $item;
    }
    private function generateDay(){
        $item;
        for($i=1;$i<=31;$i++){
            $item[$i]=$i;
        }
        return $item;
    }
}
